﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Threading;
using CasinoApp.Tools;

namespace CasinoApp.Views
{
    public partial class SlotMachinePage : PhoneApplicationPage
    {
        /// <summary>
        /// Speichert die Instanz eines SlotMachineController.
        /// </summary>
        private SlotMachineController _slotMachineController = null;

        /// <summary>
        /// Konstruktor.
        /// </summary>
        public SlotMachinePage()
        {
            InitializeComponent();

            //_SlotMachineController initialisieren.
            _slotMachineController = Application.Current.Resources["controller"] as SlotMachineController;
            _slotMachineController.StopSlotMachineRequested += new EventHandler<EventArgs>(StopSlotMachineRequestedHandler);
            _slotMachineController.SlotMachineStopped += new EventHandler<EventArgs>(SlotMachineStoppedHandler);
            _slotMachineController.StopSlotRequested += new EventHandler<StopSlotRequestedEventArgs>(StopSlotRequestedHandler);
        }

        private void startButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            _slotMachineController.StartSlotMachine(3); //Läuft 3 Sekunden

            startButton.IsEnabled = false;

            _slotMachineController.PlayPullArmSoundClip();

            slotMachineBar1.StartSpinning();
            slotMachineBar2.StartSpinning();
            slotMachineBar3.StartSpinning();

            _slotMachineController.PlayRunningWheelSoundClip();
        }

        //private void stopButton_Click(object sender, System.Windows.RoutedEventArgs e)
        //{
        //    stopButton.IsEnabled = false;
        //    startButton.IsEnabled = false;

        //    _slotMachineController.StopSlotMachine(2);
        //}

        private void StopSlotMachineRequestedHandler(object sender, EventArgs e)
        {
            startButton.IsEnabled = false;
        }

        /// <summary>
        /// Wird ausgeführt wenn ein Slot der SlotMachine angehalten werden soll.
        /// </summary>
        /// <param name="sender"><param name="sender">Das auslösende Objekt.</param></param>
        /// <param name="e">StopSlotRequestedEventArgs.</param>
        private void StopSlotRequestedHandler(object sender, StopSlotRequestedEventArgs e)
        {   
            switch (e.SlotNumber)
            {
                case (1):
                    slotMachineBar1.StopAtSymbol(_slotMachineController.SymbolsToStop[0]);
                    break;
                case (2):
                    slotMachineBar2.StopAtSymbol(_slotMachineController.SymbolsToStop[1]);
                    break;
                case (3):
                    slotMachineBar3.StopAtSymbol(_slotMachineController.SymbolsToStop[2]);
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Wird ausgeführt wenn alle Slots der SlotMachine angehalten wurden.
        /// </summary>
        /// <param name="sender"><param name="sender">Das auslösende Objekt.</param></param>
        /// <param name="e">Standard EventArgs.</param>
        private void SlotMachineStoppedHandler(object sender, EventArgs e)
        {
            startButton.IsEnabled = true;
        }

    }
}